package com.group.elmboot.service.impl;

import java.util.Random;

import com.group.elmboot.mapper.UserMapper;
import com.group.elmboot.po.User;
import com.group.elmboot.util.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.elmboot.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public User getUserByIdByPass(User user) {
		return userMapper.getUserByIdByPass(user);
	}
	
	@Override
	public int getUserById(String userId) {
		return userMapper.getUserById(userId);
	}
	
	@Override
	public int saveUser(User user) {
		return userMapper.saveUser(user);
	}
	
	@Override
	public int updateUser(User user) {
		return userMapper.updateUser(user);
	}
	
	@Override
	public int sendValidation(String userId) {
		Random rand=new Random();
		Integer validation=rand.nextInt(900000)+100000;
		userMapper.updateValidation(validation, userId);
		try {
			return SmsService.sendMessage(userId,validation);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	@Override
	public int updatePassword(User user) {
		int validation=userMapper.getValidation(user.getUserId());
		if(validation==user.getValidation())
			return userMapper.updatePassword(user);
		else
			return -1;
	}
}
